package com.hbzhit.common.exception;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.hbzhit.common.enums.REnum;
import com.hbzhit.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 自定义异常处理器
 * @author DING
 * @date 2021/4/8 17:20
 * @version 1.0
 **/
@RestControllerAdvice
@Slf4j
public class RRExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ExceptionHandler(RRException.class)
    public R<Object> handleResException(RRException e) {
        log.error(e.getMessage(), e);
        return R.error(e.getCode(), e.getMessage()).desc(e.getDesc());
    }

    @ExceptionHandler(InvalidFormatException.class)
    public R<Object> handlerInvalidFormatException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error("数据格式错误，请检查").desc(e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R<Object> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error(REnum.NOT_FOUND.getCode(), REnum.NOT_FOUND.getMsg());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R<Object> handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录").desc("违反数据库唯一性约束。");
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public R<Object> handleBadSqlGrammarException(BadSqlGrammarException e) {
        log.error(e.getMessage(), e);
        return R.error("执行SQL发生异常").desc("SQL语句执行出错。");
    }

    @ExceptionHandler(UnknownAccountException.class)
    public R<Object> handleUnknownAccountException(UnknownAccountException e) {
        log.error(e.getMessage(), e);
        return R.error("用户不存在");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public R<Object> handleIncorrectCredentialsException(IncorrectCredentialsException e) {
        log.error(e.getMessage(), e);
        return R.error("账号或密码错误");
    }

    @ExceptionHandler(LockedAccountException.class)
    public R<Object> handleLockedAccountException(LockedAccountException e) {
        log.error(e.getMessage(), e);
        return R.error("账号已被锁定，请联系管理员");
    }

    @ExceptionHandler(UnauthenticatedException.class)
    public R<Object> handleUnauthenticatedException(UnauthenticatedException e) {
        log.error(e.getMessage(), e);
        return R.error(REnum.UN_LOGIN.getCode(), REnum.UN_LOGIN.getMsg());
    }

    @ExceptionHandler(AuthorizationException.class)
    public R<Object> handleAuthorizationException(AuthorizationException e) {
        log.error(e.getMessage(), e);
        return R.error(REnum.UN_AUTH.getCode(), REnum.UN_AUTH.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public R<Object> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error().desc(e.getMessage());
    }
}
